package drone.basis.util ;

import drone.basis.dictionary.CardDict ;

public class CardUtil
{

	public static boolean isVisa (
			String card
	)
	{
		if ( card.startsWith ( "4" ) )
		{
			return true ;
		}
		return false ;
	}

	public static boolean isMaster (
			String card
	)
	{
		if ( card.startsWith ( "5" ) || card.startsWith ( "2" ) )
		{
			return true ;
		}
		return false ;
	}

	public static boolean isJcb (
			String card
	)
	{
		if ( card.startsWith ( "35" ) )
		{
			return true ;
		}
		return false ;
	}

	public static boolean isAe (
			String card
	)
	{
		if ( card.startsWith ( "34" ) || card.startsWith ( "37" ) )
		{
			return true ;
		}
		return false ;
	}

	public static boolean isClub (
			String card
	)
	{
		if ( card.startsWith ( "30" ) || card.startsWith ( "36" ) || card.startsWith ( "38" ) || card.startsWith ( "39" ) )
		{
			return true ;
		}
		return false ;
	}

	public static boolean isDiscover (
			String card
	)
	{
		if ( card.startsWith ( "60" ) )
		{
			return true ;
		}
		return false ;
	}

	// 获取名称
	public static String cardName (
			String card
	)
	{
		if ( isMaster ( card ) )
		{
			return CardDict.MASTER.name ( ) ;
		}
		if ( isVisa ( card ) )
		{
			return CardDict.VISA.name ( ) ;
		}
		if ( isAe ( card ) )
		{
			return CardDict.AE.name ( ) ;
		}
		if ( isJcb ( card ) )
		{
			return CardDict.JCB.name ( ) ;
		}
		if ( isClub ( card ) )
		{
			return CardDict.CLUB.name ( ) ;
		}
		if ( isDiscover ( card ) )
		{
			return CardDict.DISCOVER.name ( ) ;
		}
		return null ;
	}

	public static boolean legalCard (
			String card
	)
	{
		try
		{
			CardDict.valueOf ( cardName ( card ) ) ;
		} catch ( Exception e )
		{
			return false ;
		}
		if ( StringUtil.isEmpty ( card ) || ! StringUtil.isDigit ( card ) || card.length ( ) != 16 || ! verifyCard ( card ) )
		{
			return false ;
		}
		return true ;
	}

	public static boolean legalExpire (
			String expire
	)
	{
		if ( StringUtil.isEmpty ( expire ) || ! StringUtil.isDigit ( expire ) || expire.length ( ) != 4 )
		{
			return false ;
		}
		try
		{
			String yy = expire.substring ( 0 , 2 ) ;
			String mm = expire.substring ( 2 , 4 ) ;
			if ( new Integer ( mm ) > 12 || new Integer ( mm ) <= 0 )
			{
				return false ;
			}
			if ( new Integer ( yy + mm ) < 180)
			{
				return false ;
			}
		} catch ( Exception e )
		{
			return false ;
		}
		return true ;
	}

	public static boolean legalCvv (
			String cvv
	)
	{
		if ( StringUtil.isEmpty ( cvv ) || ! StringUtil.isDigit ( cvv ) || cvv.length ( ) != 3 )
		{
			return false ;
		}
		return true ;
	}

	public static String cardBin (
			String card
	)
	{
		return card.substring ( 0 , 6 ) ;
	}

	public static String cardFake (
			String card
	)
	{
		return card.substring ( 0 , 6 ) + "******" + card.substring ( 12 , 16 ) ;
	}

	public static boolean verifyCard (
			String number
	)
	{
		int s1 = 0 , s2 = 0 ;
		String reverse = new StringBuffer ( number ).reverse ( ).toString ( ) ;
		for ( int i = 0 ; i < reverse.length ( ) ; i ++ )
		{
			int digit = Character.digit ( reverse.charAt ( i ) , 10 ) ;
			if ( i % 2 == 0 )
			{
				s1 += digit ;
			} else
			{
				s2 += 2 * digit ;
				if ( digit >= 5 )
				{
					s2 -= 9 ;
				}
			}
		}
		return ( s1 + s2 ) % 10 == 0 ;
	}

}
